package com.itheima02.day04.controller;

import com.itheima02.day04.domain.Student;
import com.itheima02.day04.domain.Teacher;
import com.itheima02.day04.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author 刘全
 * @date 2021/7/19
 */
public class StudentControl {
    Scanner sc = new Scanner(System.in);
    StudentService service = new StudentService();

    //登录方法
    public void onLine() {
        while (true) {
            System.out.println("请输入用户名:");
            String s = sc.nextLine();
            System.out.println("请输入密码:");
            String num = sc.nextLine();
            boolean result = service.OnLine(s, num);
            if (result) {
                System.out.println("登录成功");
                return;
            } else {
                System.out.println("用户名或密码错误,请重新输入");
            }
        }
    }

    //黑马信息管理系统
    public void choice() {
        while (true) {
            System.out.println("-------欢迎来到黑马信息管理系统-------");
            System.out.println("1 进入学生管理系统");
            System.out.println("2 进入教师管理系统");
            System.out.println("3 退出");
            System.out.println("请输入您的选择:");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    studentChoice();
                    break;
                case 2:
                    teacherChoice();
                    break;
                case 3:
                    System.out.println("感谢您的使用!");
                    System.exit(0);
                default:
                    System.out.println("您的选择不在范围内,请重新输入!");
            }
        }
    }

    //教师管理系统
    private void teacherChoice() {
        while (true) {
            System.out.println("-------欢迎来到教师管理系统-------");
            System.out.println("1 添加教师信息");
            System.out.println("2 删除教师信息");
            System.out.println("3 修改教师信息");
            System.out.println("4 查看教师信息");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int c = sc.nextInt();
            switch (c) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    deleteTeacher();
                    break;
                case 3:
                    setTeacher();
                    break;
                case 4:
                    checkTeacher();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("您的选择不在范围内,请重新输入!");
                    break;
            }
        }
    }

    //修改教师信息的方法
    private void setTeacher() {
        Teacher[] arr = service.getArr();
        if (arr[0] == null) {
            System.out.println("系统中暂无教师信息,请添加后再修改!");
        } else {
            System.out.println("请输入要修改的教师工号:");
            String id = sc.next();
            if (service.setTeacher(id)) {
                System.out.println("修改成功!");
            } else {
                System.out.println("系统中无该教师信息,请重新输入!");
            }
        }
    }

    //删除教师信息的方法
    private void deleteTeacher() {
        Teacher[] arr = service.getArr();
        while (true) {
            if (arr[0] == null) {
                System.out.println("系统中暂无教师信息,请添加后再删除!");
                return;
            } else {
                System.out.println("请输入要删除的教师工号:");
                String id = sc.next();
                if (service.deleteTeacher(id)) {
                    System.out.println("删除成功!");
                    return;
                } else {
                    System.out.println("系统中无该教师信息,请重新输入!");
                }
            }
        }
    }

    //查看教师信息的方法
    private void checkTeacher() {
        Teacher[] arr = service.checkTeacher();
        if (arr[0] == null) {
            System.out.println("系统中暂无教师信息,请添加后再查看!");
        } else {
            System.out.println("教师工号\t\t 教师姓名\t 学生年龄\t 学生生日");
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != null) {
                    System.out.println(arr[i].getId() + "\t" + arr[i].getName() + "\t" + arr[i].getAge() + "\t" + arr[i].getBirthday());
                }
            }
        }
    }

    //添加教师信息的方法
    private void addTeacher() {
        System.out.println("请输入教师工号:");
        String id = sc.next();
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师年龄:");
        int age = sc.nextInt();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();
        Teacher teacher = new Teacher(id, name, age, birthday);
        if (service.addTeacher(teacher)) {
            System.out.println("添加成功!");
        } else {
            System.out.println("已存满");
        }
    }

    //学生管理系统
    public void studentChoice() {
        while (true) {
            System.out.println("-------欢迎来到学生管理系统-------");
            System.out.println("1 添加学生信息");
            System.out.println("2 删除学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 查看学生信息");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            int c = sc.nextInt();
            switch (c) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    setStudent();
                    break;
                case 4:
                    checkStudent();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("您的选择不在范围内,请重新输入!");
                    break;
            }
        }
    }

    //添加学生信息的方法
    public void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生学号:");
            id = sc.next();
            boolean b = service.isExist(id);
            if (b) {
                System.out.println("该学号已存在,请重新添加!");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生生日:年-月-日");
        String birthday = sc.next();
        Student student = new Student(id, name, age, birthday);
        boolean b = service.addStudent(student);
        if (b) {
            System.out.println("添加成功!");
        }
    }

    //删除学生信息的方法
    public void deleteStudent() {
        String id;
        if (service.getListSize() == 0) {
            System.out.println("系统中暂无学生信息,请添加后再删除!");
            return;
        }
        while (true) {
            System.out.println("请输入要删除的学号:");
            id = sc.next();
            boolean b = service.deleteStudent(id);
            if (b) {
                System.out.println("删除成功!");
                break;
            } else {
                System.out.println("该信息不存在,请重新输入!");
            }
        }
    }

    //修改学生信息的方法
    public void setStudent() {
        int size = service.getListSize();
        String id;
        while (true) {
            if (size == 0) {
                System.out.println("系统中暂无学生信息,请添加后修改");
                return;
            } else {
                System.out.println("请输入要修改的学号:");
                id = sc.next();
                if (service.setStudent(id)) {
                    System.out.println("修改成功!");
                    return;
                } else {
                    System.out.println("该信息不存在,请重新输入!");
                }
            }
        }
    }

    //查看学生信息的方法
    public void checkStudent() {
        ArrayList<Student> stuList = service.checkStudent();
        if (stuList.size() == 0) {
            System.out.println("系统中暂无学生信息,请添加后再查看!");
        } else {
            System.out.println("学生学号\t\t 学生姓名\t 学生年龄\t 学生生日");
            for (int i = 0; i < stuList.size(); i++) {
                System.out.println(stuList.get(i).getId() + "\t\t" + stuList.get(i).getName() + "\t\t\t" + stuList.get(i).getAge() + "\t\t\t" + stuList.get(i).getBirthday());
            }
        }
    }
}
